package site.wanjiahao.handler.server;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import site.wanjiahao.message.request.ListGroupMembersRequestMessage;
import site.wanjiahao.message.response.ListGroupMembersResponseMessage;
import site.wanjiahao.session.SessionFactory;

import java.util.Set;

@ChannelHandler.Sharable
public class ListGroupMembersRequestMessageHandler extends SimpleChannelInboundHandler<ListGroupMembersRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ListGroupMembersRequestMessage msg) throws Exception {
        Set<String> members = SessionFactory.getGroupSession().listMembers(msg.getGName());
        ListGroupMembersResponseMessage listGroupMembersResponseMessage = new ListGroupMembersResponseMessage(msg.getGName(), members);
        ctx.writeAndFlush(listGroupMembersResponseMessage);
    }
}
